{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1eda403d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store</th>\n",
       "      <th>product_group</th>\n",
       "      <th>product_code</th>\n",
       "      <th>stock_qty</th>\n",
       "      <th>cost</th>\n",
       "      <th>price</th>\n",
       "      <th>last_week_sales</th>\n",
       "      <th>last_month_sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4187</td>\n",
       "      <td>498</td>\n",
       "      <td>420.76</td>\n",
       "      <td>569.91</td>\n",
       "      <td>13</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Rose</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4195</td>\n",
       "      <td>473</td>\n",
       "      <td>545.64</td>\n",
       "      <td>712.41</td>\n",
       "      <td>16</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4204</td>\n",
       "      <td>968</td>\n",
       "      <td>640.42</td>\n",
       "      <td>854.91</td>\n",
       "      <td>22</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4219</td>\n",
       "      <td>241</td>\n",
       "      <td>869.69</td>\n",
       "      <td>1034.55</td>\n",
       "      <td>14</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4718</td>\n",
       "      <td>1401</td>\n",
       "      <td>12.54</td>\n",
       "      <td>26.59</td>\n",
       "      <td>50</td>\n",
       "      <td>285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>DaZhong</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4387</td>\n",
       "      <td>418</td>\n",
       "      <td>420.76</td>\n",
       "      <td>569.91</td>\n",
       "      <td>13</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Rose</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4295</td>\n",
       "      <td>473</td>\n",
       "      <td>506.64</td>\n",
       "      <td>712.40</td>\n",
       "      <td>16</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4504</td>\n",
       "      <td>968</td>\n",
       "      <td>580.42</td>\n",
       "      <td>854.61</td>\n",
       "      <td>62</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4219</td>\n",
       "      <td>241</td>\n",
       "      <td>679.69</td>\n",
       "      <td>1034.55</td>\n",
       "      <td>34</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4718</td>\n",
       "      <td>1201</td>\n",
       "      <td>15.54</td>\n",
       "      <td>26.59</td>\n",
       "      <td>60</td>\n",
       "      <td>285</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     store product_group  product_code  stock_qty    cost    price  last_week_sales  last_month_sales\n",
       "0   Violet           PG2          4187        498  420.76   569.91               13                58\n",
       "1     Rose           PG2          4195        473  545.64   712.41               16                58\n",
       "2   Violet           PG2          4204        968  640.42   854.91               22                88\n",
       "3    Daisy           PG2          4219        241  869.69  1034.55               14                45\n",
       "4    Daisy           PG2          4718       1401   12.54    26.59               50               285\n",
       "5  DaZhong           PG1          4387        418  420.76   569.91               13                58\n",
       "6     Rose           PG1          4295        473  506.64   712.40               16                58\n",
       "7   Violet           PG1          4504        968  580.42   854.61               62                88\n",
       "8    Daisy           PG1          4219        241  679.69  1034.55               34                45\n",
       "9    Daisy           PG1          4718       1201   15.54    26.59               60               285"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读取当前目录下data文件夹中的数据源'sales_data.csv'\n",
    "import pandas as pd\n",
    "data = pd.read_csv(\"./data/sales_data.csv\")\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "3e5850ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store</th>\n",
       "      <th>product_group</th>\n",
       "      <th>product_code</th>\n",
       "      <th>stock_qty</th>\n",
       "      <th>cost</th>\n",
       "      <th>price</th>\n",
       "      <th>last_week_sales</th>\n",
       "      <th>last_month_sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Rose</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4195</td>\n",
       "      <td>473</td>\n",
       "      <td>545.64</td>\n",
       "      <td>712.41</td>\n",
       "      <td>16</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4204</td>\n",
       "      <td>968</td>\n",
       "      <td>640.42</td>\n",
       "      <td>854.91</td>\n",
       "      <td>22</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4219</td>\n",
       "      <td>241</td>\n",
       "      <td>869.69</td>\n",
       "      <td>1034.55</td>\n",
       "      <td>14</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4718</td>\n",
       "      <td>1401</td>\n",
       "      <td>12.54</td>\n",
       "      <td>26.59</td>\n",
       "      <td>50</td>\n",
       "      <td>285</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    store product_group  product_code  stock_qty    cost    price  last_week_sales  last_month_sales\n",
       "1    Rose           PG2          4195        473  545.64   712.41               16                58\n",
       "2  Violet           PG2          4204        968  640.42   854.91               22                88\n",
       "3   Daisy           PG2          4219        241  869.69  1034.55               14                45\n",
       "4   Daisy           PG2          4718       1401   12.54    26.59               50               285"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#1.选择2行-5行所有数据\n",
    "data1 = data.loc[1:4]\n",
    "data1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "65f53fca",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'[99] not in index'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[23], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m#2.仅选择第1，100(9)行数据\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m data2 \u001b[38;5;241m=\u001b[39m data\u001b[38;5;241m.\u001b[39mloc[[\u001b[38;5;241m0\u001b[39m,\u001b[38;5;241m99\u001b[39m]]\n\u001b[0;32m      3\u001b[0m data2\n",
      "File \u001b[1;32mD:\\anaconda\\Lib\\site-packages\\pandas\\core\\indexing.py:1103\u001b[0m, in \u001b[0;36m_LocationIndexer.__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m   1100\u001b[0m axis \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39maxis \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m   1102\u001b[0m maybe_callable \u001b[38;5;241m=\u001b[39m com\u001b[38;5;241m.\u001b[39mapply_if_callable(key, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj)\n\u001b[1;32m-> 1103\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_getitem_axis(maybe_callable, axis\u001b[38;5;241m=\u001b[39maxis)\n",
      "File \u001b[1;32mD:\\anaconda\\Lib\\site-packages\\pandas\\core\\indexing.py:1332\u001b[0m, in \u001b[0;36m_LocIndexer._getitem_axis\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m   1329\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(key, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mndim\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m key\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[0;32m   1330\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot index with multidimensional key\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m-> 1332\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_getitem_iterable(key, axis\u001b[38;5;241m=\u001b[39maxis)\n\u001b[0;32m   1334\u001b[0m \u001b[38;5;66;03m# nested tuple slicing\u001b[39;00m\n\u001b[0;32m   1335\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_nested_tuple(key, labels):\n",
      "File \u001b[1;32mD:\\anaconda\\Lib\\site-packages\\pandas\\core\\indexing.py:1272\u001b[0m, in \u001b[0;36m_LocIndexer._getitem_iterable\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m   1269\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_validate_key(key, axis)\n\u001b[0;32m   1271\u001b[0m \u001b[38;5;66;03m# A collection of keys\u001b[39;00m\n\u001b[1;32m-> 1272\u001b[0m keyarr, indexer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_listlike_indexer(key, axis)\n\u001b[0;32m   1273\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_reindex_with_indexers(\n\u001b[0;32m   1274\u001b[0m     {axis: [keyarr, indexer]}, copy\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, allow_dups\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m   1275\u001b[0m )\n",
      "File \u001b[1;32mD:\\anaconda\\Lib\\site-packages\\pandas\\core\\indexing.py:1462\u001b[0m, in \u001b[0;36m_LocIndexer._get_listlike_indexer\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m   1459\u001b[0m ax \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_get_axis(axis)\n\u001b[0;32m   1460\u001b[0m axis_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39m_get_axis_name(axis)\n\u001b[1;32m-> 1462\u001b[0m keyarr, indexer \u001b[38;5;241m=\u001b[39m ax\u001b[38;5;241m.\u001b[39m_get_indexer_strict(key, axis_name)\n\u001b[0;32m   1464\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m keyarr, indexer\n",
      "File \u001b[1;32mD:\\anaconda\\Lib\\site-packages\\pandas\\core\\indexes\\base.py:5877\u001b[0m, in \u001b[0;36mIndex._get_indexer_strict\u001b[1;34m(self, key, axis_name)\u001b[0m\n\u001b[0;32m   5874\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m   5875\u001b[0m     keyarr, indexer, new_indexer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_reindex_non_unique(keyarr)\n\u001b[1;32m-> 5877\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_raise_if_missing(keyarr, indexer, axis_name)\n\u001b[0;32m   5879\u001b[0m keyarr \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtake(indexer)\n\u001b[0;32m   5880\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(key, Index):\n\u001b[0;32m   5881\u001b[0m     \u001b[38;5;66;03m# GH 42790 - Preserve name from an Index\u001b[39;00m\n",
      "File \u001b[1;32mD:\\anaconda\\Lib\\site-packages\\pandas\\core\\indexes\\base.py:5941\u001b[0m, in \u001b[0;36mIndex._raise_if_missing\u001b[1;34m(self, key, indexer, axis_name)\u001b[0m\n\u001b[0;32m   5938\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNone of [\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mkey\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m] are in the [\u001b[39m\u001b[38;5;132;01m{\u001b[39;00maxis_name\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m]\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m   5940\u001b[0m not_found \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(ensure_index(key)[missing_mask\u001b[38;5;241m.\u001b[39mnonzero()[\u001b[38;5;241m0\u001b[39m]]\u001b[38;5;241m.\u001b[39munique())\n\u001b[1;32m-> 5941\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mnot_found\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m not in index\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mKeyError\u001b[0m: '[99] not in index'"
     ]
    }
   ],
   "source": [
    "#2.仅选择第1行，100行数据\n",
    "data2 = data.loc[[0,99]]  #数据表中没有第100行，报错\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "402761cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store</th>\n",
       "      <th>product_group</th>\n",
       "      <th>product_code</th>\n",
       "      <th>stock_qty</th>\n",
       "      <th>cost</th>\n",
       "      <th>price</th>\n",
       "      <th>last_week_sales</th>\n",
       "      <th>last_month_sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4187</td>\n",
       "      <td>498</td>\n",
       "      <td>420.76</td>\n",
       "      <td>569.91</td>\n",
       "      <td>13</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4219</td>\n",
       "      <td>241</td>\n",
       "      <td>679.69</td>\n",
       "      <td>1034.55</td>\n",
       "      <td>34</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    store product_group  product_code  stock_qty    cost    price  last_week_sales  last_month_sales\n",
       "0  Violet           PG2          4187        498  420.76   569.91               13                58\n",
       "8   Daisy           PG1          4219        241  679.69  1034.55               34                45"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2.仅选择第1行，9行所有列数据\n",
    "data2 = data.loc[[0,8]]\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0fd87374",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store</th>\n",
       "      <th>product_group</th>\n",
       "      <th>product_code</th>\n",
       "      <th>stock_qty</th>\n",
       "      <th>cost</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4187</td>\n",
       "      <td>498</td>\n",
       "      <td>420.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Rose</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4195</td>\n",
       "      <td>473</td>\n",
       "      <td>545.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4204</td>\n",
       "      <td>968</td>\n",
       "      <td>640.42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4219</td>\n",
       "      <td>241</td>\n",
       "      <td>869.69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4718</td>\n",
       "      <td>1401</td>\n",
       "      <td>12.54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>DaZhong</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4387</td>\n",
       "      <td>418</td>\n",
       "      <td>420.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Rose</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4295</td>\n",
       "      <td>473</td>\n",
       "      <td>506.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4504</td>\n",
       "      <td>968</td>\n",
       "      <td>580.42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4219</td>\n",
       "      <td>241</td>\n",
       "      <td>679.69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG1</td>\n",
       "      <td>4718</td>\n",
       "      <td>1201</td>\n",
       "      <td>15.54</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     store product_group  product_code  stock_qty    cost\n",
       "0   Violet           PG2          4187        498  420.76\n",
       "1     Rose           PG2          4195        473  545.64\n",
       "2   Violet           PG2          4204        968  640.42\n",
       "3    Daisy           PG2          4219        241  869.69\n",
       "4    Daisy           PG2          4718       1401   12.54\n",
       "5  DaZhong           PG1          4387        418  420.76\n",
       "6     Rose           PG1          4295        473  506.64\n",
       "7   Violet           PG1          4504        968  580.42\n",
       "8    Daisy           PG1          4219        241  679.69\n",
       "9    Daisy           PG1          4718       1201   15.54"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#3.选取标签名为“store”到“cost”之间的列\n",
    "#loc时标签索引\n",
    "data3 = data.loc[:,:'cost']\n",
    "data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6f8845a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store</th>\n",
       "      <th>cost</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Violet</td>\n",
       "      <td>420.76</td>\n",
       "      <td>569.91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Rose</td>\n",
       "      <td>545.64</td>\n",
       "      <td>712.41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Violet</td>\n",
       "      <td>640.42</td>\n",
       "      <td>854.91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>869.69</td>\n",
       "      <td>1034.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>12.54</td>\n",
       "      <td>26.59</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>DaZhong</td>\n",
       "      <td>420.76</td>\n",
       "      <td>569.91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Rose</td>\n",
       "      <td>506.64</td>\n",
       "      <td>712.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Violet</td>\n",
       "      <td>580.42</td>\n",
       "      <td>854.61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>679.69</td>\n",
       "      <td>1034.55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>15.54</td>\n",
       "      <td>26.59</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     store    cost    price\n",
       "0   Violet  420.76   569.91\n",
       "1     Rose  545.64   712.41\n",
       "2   Violet  640.42   854.91\n",
       "3    Daisy  869.69  1034.55\n",
       "4    Daisy   12.54    26.59\n",
       "5  DaZhong  420.76   569.91\n",
       "6     Rose  506.64   712.40\n",
       "7   Violet  580.42   854.61\n",
       "8    Daisy  679.69  1034.55\n",
       "9    Daisy   15.54    26.59"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#4.仅选择标签名为'store','cost',''price'3列数据\n",
    "data4 = data[['store','cost','price']]  #花式索引选取多列\n",
    "data4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6d0bac10",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store</th>\n",
       "      <th>cost</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Violet</td>\n",
       "      <td>420.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Rose</td>\n",
       "      <td>545.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Violet</td>\n",
       "      <td>640.42</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    store    cost\n",
       "0  Violet  420.76\n",
       "1    Rose  545.64\n",
       "2  Violet  640.42"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#5.选取第1到3行'store','cost'两列\n",
    "data5 = data.loc[0:2,['store','cost']]\n",
    "data5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "d354d1e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store</th>\n",
       "      <th>product_group</th>\n",
       "      <th>product_code</th>\n",
       "      <th>stock_qty</th>\n",
       "      <th>cost</th>\n",
       "      <th>price</th>\n",
       "      <th>last_week_sales</th>\n",
       "      <th>last_month_sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4187</td>\n",
       "      <td>498</td>\n",
       "      <td>420.76</td>\n",
       "      <td>569.91</td>\n",
       "      <td>13</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Rose</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4195</td>\n",
       "      <td>473</td>\n",
       "      <td>545.64</td>\n",
       "      <td>712.41</td>\n",
       "      <td>16</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4204</td>\n",
       "      <td>968</td>\n",
       "      <td>640.42</td>\n",
       "      <td>854.91</td>\n",
       "      <td>22</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4219</td>\n",
       "      <td>241</td>\n",
       "      <td>869.69</td>\n",
       "      <td>1034.55</td>\n",
       "      <td>14</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    store product_group  product_code  stock_qty    cost    price  last_week_sales  last_month_sales\n",
       "0  Violet           PG2          4187        498  420.76   569.91               13                58\n",
       "1    Rose           PG2          4195        473  545.64   712.41               16                58\n",
       "2  Violet           PG2          4204        968  640.42   854.91               22                88\n",
       "3   Daisy           PG2          4219        241  869.69  1034.55               14                45"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#6.选择第1，2，3，4行所有列\n",
    "data6 = data.loc[0:3]\n",
    "data6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "44051ada",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store</th>\n",
       "      <th>product_group</th>\n",
       "      <th>product_code</th>\n",
       "      <th>stock_qty</th>\n",
       "      <th>cost</th>\n",
       "      <th>price</th>\n",
       "      <th>last_week_sales</th>\n",
       "      <th>last_month_sales</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4187</td>\n",
       "      <td>498</td>\n",
       "      <td>420.76</td>\n",
       "      <td>569.91</td>\n",
       "      <td>13</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG2</td>\n",
       "      <td>4718</td>\n",
       "      <td>1401</td>\n",
       "      <td>12.54</td>\n",
       "      <td>26.59</td>\n",
       "      <td>50</td>\n",
       "      <td>285</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    store product_group  product_code  stock_qty    cost   price  last_week_sales  last_month_sales\n",
       "0  Violet           PG2          4187        498  420.76  569.91               13                58\n",
       "4   Daisy           PG2          4718       1401   12.54   26.59               50               285"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#7.仅选择1，5行的所有列\n",
    "data7 = data.loc[[0,4]]\n",
    "data7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "7e0f5474",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store</th>\n",
       "      <th>product_group</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Rose</td>\n",
       "      <td>PG2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>DaZhong</td>\n",
       "      <td>PG1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Rose</td>\n",
       "      <td>PG1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Violet</td>\n",
       "      <td>PG1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>PG1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     store product_group\n",
       "0   Violet           PG2\n",
       "1     Rose           PG2\n",
       "2   Violet           PG2\n",
       "3    Daisy           PG2\n",
       "4    Daisy           PG2\n",
       "5  DaZhong           PG1\n",
       "6     Rose           PG1\n",
       "7   Violet           PG1\n",
       "8    Daisy           PG1\n",
       "9    Daisy           PG1"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#8.读取所有行中0至2中的两列\n",
    "#使用位置选取列时使用iloc\n",
    "data8 = data.iloc[:,0:2]\n",
    "data8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "adce7bc1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>store</th>\n",
       "      <th>product_code</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Violet</td>\n",
       "      <td>4187</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Rose</td>\n",
       "      <td>4195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Violet</td>\n",
       "      <td>4204</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>4219</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>4718</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>DaZhong</td>\n",
       "      <td>4387</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Rose</td>\n",
       "      <td>4295</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Violet</td>\n",
       "      <td>4504</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>4219</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Daisy</td>\n",
       "      <td>4718</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     store  product_code\n",
       "0   Violet          4187\n",
       "1     Rose          4195\n",
       "2   Violet          4204\n",
       "3    Daisy          4219\n",
       "4    Daisy          4718\n",
       "5  DaZhong          4387\n",
       "6     Rose          4295\n",
       "7   Violet          4504\n",
       "8    Daisy          4219\n",
       "9    Daisy          4718"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#9.仅选择第0，2列\n",
    "data9 = data.iloc[:,[0,2]]\n",
    "data9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3edca3bc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
